import java.awt.*;

/**
 * @authoer:wulongbo
 * @createDate:2022/8/10
 * @description:
 */
public class VerticalBisector {
    public static void main(String[] args) {
//        // 求两点间的垂直平分线
//        // 设点A(x1,.y1),点B(x2,y2),设AB的中点M(x0,y0)
//        Point A = new Point(10, 20);// 定义第一个点的坐标(10,20)
//        Point B = new Point(70, 80);// 定义第二个点的坐标(70,80)
//        Point M= new Point((int) ((A.getX()+B.getX())/2), (int) ((A.getY()+B.getY())/2));
//        // 计算AB的斜率
//        double k1 = (B.getY() - A.getY()) / (B.getX() - A.getX());
//        // 计算垂直平分线的斜率
//        double k = -((B.getX() - A.getX())/(B.getY() - A.getY())) ;
        Calculate(10f,20f,70f,80f);
        // 方程式为： y = k2*x + b2


    }

    public static int Calculate( float x1,  float x2,  float y1,  float y2) {
        float k1 = 1; //已知坐标点直线的斜率k值
        float b1 = 1; //已知坐标点直线的纵截距b值
        float k2 = 1; //垂直平分线的斜率k值
        float b2 = 1; //垂直平分线的纵截距b值

        try {
            //特殊线1
            if (x1 == x2) {
                float center = (y1 + y2) / 2;//中心点
                float dist = Math.abs(y1 - center);//端点到中心点的距离值
                x1 = center - dist;
                x2 = center + dist;
                y2 = y1 = (x1 + x2) / 2;
                return 0;
            }
            //特殊线2
            if (y1 == y2) {
                float center = (x1 + x2) / 2;//中心点
                float dist = Math.abs(x1 - center);//端点到中心点的距离值
                x1 = x2 = (y1 + y2) / 2;
                y1 = center - dist;
                y2 = center + dist;
                return 0;
            }
            float temp = 0;
            float coefficient; //系数值
            if (x1 >= x2) {
                coefficient = (float) Math.floor(((x1 / x2)));
                temp = y2 * coefficient; //将对应的函数乘以系数
                b1 = (float) Math.floor(((temp - y1) / (coefficient - 1))); //求出b值
                k1 = (float) Math.floor(((y1 - b1) / x1)); //求出k值
            } else {
                coefficient = x2 / x1;
                temp = y1 * coefficient;
                b1 = (float) Math.floor(((temp - y2) / (coefficient - 1))); //求出b值
                k1 = (float) Math.floor(((y2 - b1) / x2)); //求出k值
            }
            k2 = -1 / k1; //垂直平分线的斜率k值
            b2 = (x1 + x2) / (2 * k1) + (y1 + y2) / 2; //垂直平分线的纵截距b值
            double cenX = (x1 + x2) / 2;//交点x
            double cenY = (y1 + y2) / 2;//交点y
            double dist = Math.sqrt(Math.abs(x1 - x2) * Math.abs(x1 - x2) + Math.abs(y1 - y2) * Math.abs(y1 - y2)) / 2;//每个点距离交点(中心点)距离相同
            double douAngle = Math.atan(k2);//垂直平分线和X轴的夹角

            //计算起始点和终止点
            x1 = (float)(cenX + dist * Math.cos(douAngle));
            x2 = (float)(cenX - dist * Math.cos(douAngle));
            y1 = (float)(cenY + dist * Math.sin(douAngle));
            y2 = (float)(cenY - dist * Math.sin(douAngle));
            // 方程式为： y = k2*x + b2
            System.out.printf("k2的值为%f,b2的值为%f", k2,b2);
            System.out.println();
            System.out.printf("中点M(15,75),x0=15带人方程 y = k2*x + b2 计算y的结果为%f", k2*15 + b2);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

}
